package ae.pcfc.etks.online.itworx.ehs.web.delegate.impl;

import java.util.ArrayList;
import java.util.List;

import ae.pcfc.etks.online.itworx.ehs.web.delegate.IMSDelegate;
import ae.pcfc.etks.online.itworx.ehs.web.entity.Comment;
import ae.pcfc.etks.online.itworx.ehs.web.enums.IMSRequestStatus;
import ae.pcfc.etks.online.itworx.ehs.web.exception.CommunicationException;
import ae.pcfc.etks.online.itworx.ehs.web.exception.IMSException;
import ae.pcfc.etks.online.itworx.ehs.web.facade.IMSFacade;
import ae.pcfc.etks.online.itworx.ehs.web.model.IMSComment;

public class IMSDelegateImpl extends BaseDelegateImpl implements IMSDelegate {

	
	
	@Override
	public String createIMPSRecord(String inspectionCode, String category,
			String buCode, String requestorId, String createdBy) throws IMSException, CommunicationException {
		return serviceLocator.getFacadeService(IMSFacade.class).createIMPSRecord(inspectionCode, category, buCode, requestorId, createdBy);
	}

	@Override
	public String cancelIMPSRecord(String imsLogId) throws IMSException, CommunicationException {
		return serviceLocator.getFacadeService(IMSFacade.class).cancelIMPSRecord(imsLogId);
	}

	@Override
	public IMSRequestStatus retrieveIMSStatus(String imsLogId) throws IMSException, CommunicationException {
		String imsRequestStatus = serviceLocator.getFacadeService(IMSFacade.class).retrieveIMSStatus(imsLogId);
		return IMSRequestStatus.getStatus(imsRequestStatus);
	}

	@Override
	public List<Comment> retrieveIMSComments(String imsLogId) throws IMSException, CommunicationException {
		List<IMSComment> imsComments = serviceLocator.getFacadeService(IMSFacade.class).retrieveIMSComments(imsLogId);
		List<Comment> comments = new ArrayList<Comment>();
		for(IMSComment imsComment:imsComments){
			Comment comment = new Comment();
			mapper.map(imsComment, comment);
			comments.add(comment);
		}
		return comments; 
	}

	@Override
	public String setIMSCustomerReply(String imsLogId,List<Comment> comments) throws IMSException,CommunicationException {
		List<IMSComment> imsComments = new ArrayList<IMSComment>();
		for(Comment comment:comments){
			IMSComment imsComment = new IMSComment();
			mapper.map(comment, imsComment);
			imsComments.add(imsComment);
		}
		return serviceLocator.getFacadeService(IMSFacade.class).setIMSCustomerReply(imsLogId, imsComments);
	}
	
}
